const permission = {
  inserted(el, binding, vnode) {
    const {
      meta: { permissionCodes },
      permissionCode
    } = binding.value
    const hasPermission = permissionCodes instanceof Array && permissionCodes.length > 0 && permissionCodes.includes(permissionCode)
    if (!hasPermission) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

const install = function(Vue) {
  Vue.directive('permission', permission)
}

if (window.Vue) {
  window['permission'] = permission
  Vue.use(install) // eslint-disable-line
}

permission.install = install

export default permission
